//
//  MyPlistCacheBehavior.swift
//  RIBsTest
//
//  Created by lvzhao on 2024/5/25.
//

import Foundation

///plist 存储
open class MyPlistCacheBehavior: MyCacheBehavior{
    public func set<T>(key: String, data: T, isEncrypt: Bool) -> MyStoreErrorCode {
      
        ///暂时忽略加密
        return self.checkSetValidity(key: key, data: data)
    }
    
    public func get<T>(key: String, isDecrypt: Bool) -> T?{
        ///暂时忽略加密
        return self.checkGetDataValidity(fileName: key)

    }
}


//MARK - 路径相关
public extension MyPlistCacheBehavior {
    ///  获取文件路径
    /// - Parameters:
    ///   - xxx: xxxxxx
    /// - Returns: 加密后生成的字符串
    ///
    
    func dataFilePath(fileName: String, path: MyDataCachePath) -> String{
        return NSHomeDirectory().appending(path.rawValue).appending(self.makePlistPath(fileName))
    }
    
    func makePlistPath(_ fileName: String) -> String {
        if fileName.contains(".plist") {
            return fileName
        } else {
            return "\(fileName).plist"
        }
    }
}

/// MARK - 存储行为
extension MyPlistCacheBehavior{
    func setNSArray(fileName: String, data: NSArray, path:MyDataCachePath = .document) -> MyStoreErrorCode {
        if data.write(toFile: self.dataFilePath(fileName: fileName, path: path), atomically: true) {
            return .success
        } else {
            return .setError
        }
    }
    
    func setNSDictionary(fileName: String, data:NSDictionary, path: MyDataCachePath = .document) -> MyStoreErrorCode {
        if data.write(toFile: self.dataFilePath(fileName: fileName, path: path), atomically: true) {
            return .success
        } else {
            return .setError
        }
    }
    
    
    func setNSData(fileName: String, data:NSData, path: MyDataCachePath = .document) -> MyStoreErrorCode {
        if data.write(toFile: self.dataFilePath(fileName: fileName, path: path), atomically: true){
            return .success
        }
        else {
            return .setError
        }
    }
    
    
    func getNSArray(fileName: String, path: MyDataCachePath = .document) -> NSArray? {
        guard let data = NSArray(contentsOfFile: self.dataFilePath(fileName: fileName, path: path)) else {return nil}
        return data
    }
    
    func getNSDictionary(fileName: String, path: MyDataCachePath = .document) -> NSDictionary? {
        guard let data = NSDictionary(contentsOfFile: self.dataFilePath(fileName: fileName, path: path)) else {return nil}
        return data
    }
    
    func getNSData(fileName: String, path: MyDataCachePath = .document) -> NSData? {
        guard let data = NSData(contentsOfFile: self.dataFilePath(fileName: fileName, path: path)) else {return nil}
        return data
    }
    
}






extension MyPlistCacheBehavior {
    
    
    /// 私有函数获取缓存的数据
    /// - Parameters:
    ///   - fileName: 文件名称
    ///   - path: 根目录路径
    /// - Returns: 缓存的数据
    private func checkGetDataValidity<T>(fileName: String,  path: MyDataCachePath = .document) -> T? {
        if let data = self.getNSArray(fileName: fileName, path: path){
            guard let item = data as? T else { return nil }
            return item
        } else if let data = self.getNSDictionary(fileName: fileName, path: path) {
            guard let item = data as? T else { return nil }
            return item
        } else if let data = self.getNSData(fileName: fileName, path: path) {
            guard let item = data as? T else { return nil }
            return item
        } else {
            return nil
        }
            
    }
    
    
    private func checkSetValidity<T>(key: String, data: T) -> MyStoreErrorCode {
        if let item = data as? NSArray {
            return self.setNSArray(fileName: key, data: item, path: .document)
        } else if let item = data as? NSDictionary {
            return self.setNSDictionary(fileName: key, data: item, path: .document)
        } else if let item = data as? NSData {
            return self.setNSData(fileName: key, data: item, path: .document)
        } else {
            return .typeError
        }
    }
}

